package com.bhs.sansong.ui.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansong.R;
import com.bhs.sansong.bean.TypeBean;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lxj.xpopup.impl.PartShadowPopupView;

import java.util.List;

/**
 * 筛选
 */
public class FiltratePopupView extends PartShadowPopupView {

    private RecyclerView mRvList;
    private BaseQuickAdapter<TypeBean, BaseViewHolder> mAdapter;
    private List<TypeBean> mList;
    private int type;

    public FiltratePopupView(@NonNull Context context, List<TypeBean> mList, int type) {
        super(context);
        this.mList = mList;
        this.type = type;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_flitrate;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();
    }

    private void initView() {
        mRvList = findViewById(R.id.rv_list);
        int layoutId = 0;
        RecyclerView.LayoutManager layout;
        if (type == 1) {
            layoutId = R.layout.item_filtrate;
            layout = new LinearLayoutManager(getContext());
        } else if (type == 2) {
            layoutId = R.layout.item_filtrate2;
            layout = new GridLayoutManager(getContext(), 2);
        } else {
            layoutId = R.layout.item_filtrate3;
            layout = new LinearLayoutManager(getContext());
        }
        mAdapter = new BaseQuickAdapter<TypeBean, BaseViewHolder>(layoutId, mList) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, TypeBean item) {
                helper.setText(R.id.tv_content, item.getTypeName());
                helper.setTextColor(R.id.tv_content, Color.parseColor(item.isChecked() ? "#485EF4" : "#000000"));
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(layout);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TypeBean data = mAdapter.getData().get(position);
                for (TypeBean datum : mAdapter.getData()) {
                    datum.setChecked(false);
                }
                data.setChecked(true);
                listener.onClick(data.getType(), data.getTypeName());
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    private MyClickListener2 listener;

    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.listener = l;
    }
}
